$(function(){



    $("#dept_datagrid").datagrid({
        width:500,
        height:400,
        url:"/botany/query.do",
        columns:[
            [
                {field:'men',title:'门',width:100},
                {field:'gang',title:'纲',width:100},
                {field:'mu',title:'目',width:100},
                {field:'ke',title:'科',width:100},
                {field:'shu',title:'属',width:100},
                {field:'shu',title:'种',width:100},
                {field:'name',title:'俗名',width:100},
                {field:'picture',title:'图片',width:70,formatter: function (value,row,index) {
                        var smal = value.slice(34,value.length)
                        var s =  "../../upload/"+smal.substring(0,36)+"_small"+smal.substring(36,smal.length);
                        return value ? "<a class=\"fancybox\" data-fancybox-group=\"gallery\" title="+row.name+" href="+s+">" +
                            "<img style='width: 100px;height: 100px' src=" + value + "></a>": "";
                    }},
                {field:'description',title:'描述',width:100},
                {field:'user',title:'录入人',width:100},
            ]
        ],
        toolbar:'#toolbar',
        fitColumns:true,
        striped:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        fit:true
    })


    $("#dept_dialog").dialog({
        width:700,
        height:600,
        closed:true,
        buttons:'#dept_btns',
        onClose:function(){
            $("#dept_form").form("clear");
        }
    })




    $("a[data-cmd]").on('click',function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})



function add(){
    $("#dept_form").form("clear");
    $("#pwd").show();
    $("#dept_dialog").dialog("open");
    $("#dept_dialog").dialog("setTitle", "新增资料");

}

function cancel(){
    $("#dept_dialog").dialog("close");
}

function save(){
    $("#dept_form").form({
        url:"/botany/saveOrUpdate.do",
        success:function(data){
            data = $.parseJSON(data);
            if(data.success){
                $.messager.alert('温馨提示', '保存成功', 'info', function() {
                    $("#dept_dialog").dialog('close');
                    $("#dept_datagrid").datagrid('reload');
                })
            } else {
                $.messager.alert('温馨提示', '保存失败', 'error')
            }
        }

    })
    $('#dept_form').submit();
}

function edit(){
    $("#dept_form").form("clear");
    var row = $("#dept_datagrid").datagrid("getSelected");

    if (!row){
        $.messager.alert("温馨提示", "至少选中一个资料", "warning");
        return;
    }

    $("#dept_form").form("load", row);

    $("#dept_dialog").dialog("open");
    $("#dept_dialog").dialog("setTitle", "编辑资料");

}

function remove(){
    var row = $("#dept_datagrid").datagrid("getSelected");

    if(!row){
        $.messager.alert("温馨提示", "至少选中一个资料", "warning");
    }

    $.messager.confirm("确定对话框", "确定删除吗", function (r) {
        if (r){
            $.get('/botany/delete.do', {id:row.id}, function (data) {
                if (data.success){
                    $.messager.alert("温馨提示", "删除成功", "info", function () {
                        $("#dept_datagrid").datagrid("load");
                    });
                } else {
                    $.messager.alert("温馨提示", "删除失败", "errot");
                }
            }, 'json');
        }
    })

}

function reload(){
    $("#dept_datagrid").datagrid("load");
}

function searching() {
    var val = $("#keyword").textbox('getValue');
    var classifs = $("#classifs").textbox('getValue');
    $("#dept_datagrid").datagrid("load", {
        keyword: val,
        classifs: classifs
    })
}